﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lib.Utils;

namespace MS.INV.MainData
{
    public partial class Tables : Lib.Bases.Forms.BaseForm
    {
        SysLst.L_Ban tbl = new SysLst.L_Ban();

        public Tables()
        {
            InitializeComponent();

            cbViTri.BaseList = new SysLst.L_ViTriBan();
        }

        protected override void OnHandleDestroyed(EventArgs e)
        {
            tbl.Dispose();
            base.OnHandleDestroyed(e);
        }

        private void Tables_Load(object sender, EventArgs e)
        {

        }

        private void cbViTri_EditValueChanged(object sender, EventArgs e)
        {
            grid.SetData(tbl.GetDataByPosId(Cvert.ToInt32(cbViTri.EditValue, 0)));
        }

        private void btCreate_Click(object sender, EventArgs e)
        {
            if (nuNumber.Value <= 0) return;
            DataTable Data = (DataTable)grid.DataSource;
            if (Data == null) return;

            for (int i = 0; i < nuNumber.Value; i++)
            {
                DataRow row = Data.NewRow();
                row["TenBan"] = txtBegin.Text.Trim() + (i + 1 + spBeginNumber.Value).ToString();
                row["IdViTri"] = Cvert.ToInt32(cbViTri.EditValue, 0);
                Data.Rows.Add(row);
            }
        }

        private void btnUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                DataTable Data = grid.GetAcceptChanges();
                if (Data == null) return;
                tbl.Update(ref Data);
                btnUpdate.Enabled = false;
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Update");
            }
        }

        private void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn có chắc chắn muốn xóa dòng dữ liệu này không ?", "Xác nhận !",
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) return;

                DataRow row = view.GetFocusedDataRow();
                if (object.Equals(row, null)) return;
                tbl.DeleteRow(row);
                view.DeleteSelectedRows();
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Remove Item.");
            }
        }

        private void btnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void view_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            btnUpdate.Enabled = true;
        }
    }
}
